package com.zhw.service.impl;

import com.zhw.mapper.DeptMapper;
import com.zhw.mapper.EmpMapper;
import com.zhw.pojo.Dept;
import com.zhw.pojo.DeptLog;
import com.zhw.service.DeptLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Transactional
public class DeptServiceImpl implements com.zhw.service.DeptService {

    @Autowired
    DeptMapper dm;

    @Autowired
    EmpMapper em;


    @Autowired
    DeptLogService dls;


    @Override
    public List<Dept> getDept() {
        List<Dept> dept = dm.getDept();
        return dept;

    }

    @Override
    public void delete(int id) {
        try {
            dm.delete(id);
            int a=1/0;
            //连同这个部门下的所有人一起删除掉
            em.deleteById(id);
        } finally {
            DeptLog dl=new DeptLog();
            dl.setCreateTime(LocalDateTime.now());
            dl.setLog("id为"+id+"被删除");
            dls.Insert(dl);
        }

    }

    @Override
    public void add(Dept dept) {

        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        dm.add(dept);
    }

    @Override
    public Dept selectById(Integer id) {
        Dept dept=dm.selectById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dm.update(dept);
    }
}
